<template>
  <div class="student-container">
    <van-nav-bar
      fixed
      left-arrow
      @click-left="
        $router.back()
        socked.disconnect()
      "
      title="小智同学"
    ></van-nav-bar>
    <div class="chat-list" ref="name">
      <template v-for="(item, index) in list">
        <!-- 左侧是机器人小智 -->
        <div class="chat-item left" v-if="item.isStu" :key="index">
          <van-image fit="cover" round :src="xiaozhi" />
          <div class="chat-pao">{{ item.msg }}</div>
        </div>
        <!-- 右侧是当前用户 -->
        <div class="chat-item right" v-else :key="index + 1">
          <div class="chat-pao my">{{ item.msg }}</div>
          <van-image fit="cover" round :src="userPhoto" />
        </div>
      </template>
    </div>
    <div class="reply-container van-hairline--top">
      <van-search v-model="msg" @search="onSearch" placeholder="说点什么...">
      </van-search>
    </div>
  </div>
</template>

<script>
// 导入图片
import xiaozhi from '../../assets/xiaozhi.webp'
import yonghu from '../../assets/01.jpg'
// 导入socket.io-client框架
import io from 'socket.io-client'
export default {
  name: 'student',
  data () {
    return {
      xiaozhi,
      socked: null, //与服务器的链接
      msg: '', // 输入框内容
      list: [], // 聊天数据
      userPhoto: this.$store.state.userInfo.photo
    }
  },
  // 声明周期钩子  获取服务器内容
  created () {
    this.socked = io('http://118.24.62.36:8888') // 链接服务器
    // 链接成功的回调函数
    this.socked.on('connect', () => {
      // console.log('链接成功')
    })
    // 接收服务器的消息
    this.socked.on('message', data => {
      console.log(data)
      // 向数组中添加数据
      this.list.push({
        msg: data.msg,
        isStu: true
      })
    })
    console.log()
  },
  methods: {
    // 搜索框事件 向服务器发送内容
    onSearch () {
      if (this.msg == '') {
        this.$dialog.alert({
          title: '温馨提示',
          message: '说点啥吧...'
        })
        return
      }
      // 给服务器发消息
      this.socked.emit('message', {
        // 消息
        msg: this.msg,
        // 时间戳
        timestamp: Date.now()
      })
      // 更新list数组
      this.list.push({
        msg: this.msg,
        isStu: false,
        timestamp: Date.now()
      })
      // 清空输入框的内容
      this.msg = ''
    }
  },
  // 生命周期钩子
  updated () {
    // 自动滚动
    this.$nextTick(function () {
      this.$refs.name.scrollTop = this.$refs.name.scrollHeight
    })
  }
}
</script>

<style lang="less">
.student-container {
  height: 100%;
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  box-sizing: border-box;
  background: #fafafa;
  padding: 46px 0 50px 0;
  .van-nav-bar {
    background-color: #3296fa;
    .van-nav-bar__title {
      color: white;
    }
    .van-icon {
      color: white;
    }
  }
  .chat-list {
    height: 100%;
    overflow-y: scroll;
    .chat-item {
      padding: 10px;
      .van-image {
        vertical-align: top;
        width: 40px;
        height: 40px;
      }
      .chat-pao {
        vertical-align: top;
        display: inline-block;
        min-width: 40px;
        max-width: 70%;
        min-height: 40px;
        line-height: 38px;
        border: 0.5px solid #c2d9ea;
        border-radius: 4px;
        position: relative;
        padding: 0 10px;
        background-color: #e0effb;
        word-break: break-all;
        font-size: 14px;
        color: #333;
        &::before {
          content: '';
          width: 10px;
          height: 10px;
          position: absolute;
          top: 12px;
          border-top: 0.5px solid #c2d9ea;
          border-right: 0.5px solid #c2d9ea;
          background: #e0effb;
        }
      }
      .chat-pao.my {
        background-color: #9eea6a;
        &::before {
          content: '';
          background: #9eea6a;
        }
      }
    }
  }
  .chat-item.right {
    text-align: right;
    .chat-pao {
      margin-left: 0;
      margin-right: 15px;
      &::before {
        right: -6px;
        transform: rotate(45deg);
      }
    }
  }
  .chat-item.left {
    text-align: left;
    .chat-pao {
      margin-left: 15px;
      margin-right: 0;
      &::before {
        left: -5px;
        transform: rotate(-135deg);
      }
    }
  }
  .reply-container {
    position: fixed;
    left: 0;
    bottom: 0;
    height: 44px;
    width: 100%;
    background: #f5f5f5;
    z-index: 9999;
  }
}
</style>
